import { createRouter, createWebHashHistory } from "vue-router";
import routes from "./routes";

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 路由前置守卫
router.beforeEach((to, from, next) => {
  const username = sessionStorage.getItem("username") || "";
  if (username) {
    next();
  } else {
    if (to.fullPath !== "/login") {
      next("/login");
    } else {
      next();
    }
  }
});

// 路由后置守卫, 无next形参
router.afterEach((to, from) => {
  if (to.path === "/login") {
    document.title = "登录页";
  } else {
    document.title = "后台管理系统";
  }
});

export default router;
